'use strict';

import { ajax,nameSpace } from 'utils/index';

let ns = nameSpace('Models');
export const GET_MODELS_DONE = ns('GET_MODELS_DONE');
export const GET_SHOP_MODELS_DONE = ns('GET_SHOP_MODELS_DONE');
export const BUY_MODEL_DONE = ns('BUY_MODEL_DONE');
export const LOAD_START = ns('LOAD_START');
export const LOAD_ERR = ns('LOAD_ERR');
export const SHOW_MODELS_DIALOG = ns('SHOW_MODELS_DIALOG');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const SHOW_BUY_URL = ns('SHOW_BUY_URL');
export const CREATE_PAGE_DONE = ns('CREATE_PAGE_DONE');
export const ADD_TEXT = ns('ADD_TEXT');

export function createPage(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'createPage',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:CREATE_PAGE_DONE,
				  page_id:json.page_id
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function getShopModels(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'getShopModels',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:GET_SHOP_MODELS_DONE,
				  data:json,
				  params
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function getModels(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'getPageModels',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:GET_MODELS_DONE,
				  data:json,
				  params
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function buyModel(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'buyPageModels',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				type: LOAD_ERR,
				message:json.msg,
				loginFalse:false
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  if(json.code==930){
				dispatch({
					type: SHOW_BUY_URL,
					message:json.msg,
					url:json.url,
					buyDialog:true,
					oid:json.oid
				});
				// window.open(json.url); 
				return;
			  }
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function checkBuyModel(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'checkBuyModel',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  if(json.is_buy=="true"){
				dispatch({
					  type:BUY_MODEL_DONE,
					  message:json.msg,
				  });
			  }else{
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:false
				});
			  }
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  if(json.code==930){
				dispatch({
					type: SHOW_BUY_URL,
					message:json.msg,
					url:json.url,
					buyDialog:true
				});
				window.open(json.url); 
				return;
			  }
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function closeSystemDialog(isShow) {
	return {
    type: CLOSE_DIALOG
  };
}
export function showModelsDialog(isShow) {
	return {
    type: SHOW_MODELS_DIALOG,
    moreModelsDialog: isShow
  };
}
export function buyDialogClose(isShow) {
	return {
    type: SHOW_BUY_URL,
    buyDialog: isShow
  };
}
export function addText(text) {
  return {
    type: ADD_TEXT,
    data: text
  };
}


